<template>
  <div class="sim-console">
    <div v-scorllBottom="isOnArea" class="_panel-content" @mousemove="handleMouseover" @mouseleave="handleMouseleave">
      <div 
        v-for="(item) in renderData.records"
        :key="item.id"
        class="g-flex"
        :class="{'warn-text': item.level === 'WARN', 'error-text': item.level === 'ERROR', }"
        style="font-size:12px">
        <span class="__log-time g-flex-0 g-m-r-10">{{ item.time }}</span>
        <span class="__log-text g-flex-1"> {{ item.context }}</span>
      </div>

    </div>
    <div class="bottom_input">
      <span class="prepend"><span>matlab</span>>></span>
      <el-input v-model="emulateText" :disabled=isCommandDisabled v-on:keyup.enter="submitEmulateText"></el-input><span
        class="append">
        <sim-dropdown>
          Matlab<img src="statics/images/mainpage/selectLogo.svg" alt="">
          <template #dropdown>
            <SimDropdownItem
              @click="changeStatus('MATLAB')">
              <span>MATLAB</span>
            </SimDropdownItem>
          </template>
        </sim-dropdown>
      </span>
    </div>

  </div>
</template>
<script lang="ts">
import { ElNotification } from "element-plus";
import { defineComponent, ref, toRefs } from "vue";
import { controller, SimulateController } from "@/engine/Controller";
import ConsoleService from "@/engine/ConsoleService";
import SimDropdown from "@/components/dropdown/Dropdown.vue";
import SimDropdownItem from "@/components/dropdown/DropdownMenuItem.vue";
import local from "@/util/local";
export default defineComponent({
  name: "sim-console",
  components: {
    SimDropdown,
    SimDropdownItem
  },
  directives: {
    scorllBottom: {
      mounted(el) {
      },
      updated(el, binding) {
        if (!binding.value) {
          el.scrollTop = el.scrollHeight;
        }
      }
    }
  },
  setup() {
    const isOnArea = ref(false);
    const handleMouseover = () => {
      isOnArea.value = true;
    };
    const handleMouseleave = () => {
      isOnArea.value = false;
    };
    let emulateText = ref("");
    let langType = ref("MATLAB");
    let isCommandDisabled = ref(false);
    let renderData = controller.consoleService.renderData;
    return {
      handleMouseleave,
      handleMouseover,
      isOnArea,
      emulateText,
      langType,
      isCommandDisabled,
      renderData
    };
  },
  computed: {
    computedController(): SimulateController {
      console.log("?");
      return controller;
    },
    consoleService(): ConsoleService {
      return this.computedController.consoleService;
    }
  },
  methods: {
    changeStatus(value:string){
      this.langType =  value;
    },
    clearLog() {
      this.consoleService.clearLog();
    },
    copyLog() {
      this.consoleService.copyLog();
    },
    exportLog() {
      this.consoleService.exportLog();
    },
    submitEmulateText() {
      this.isCommandDisabled = true;
      this.consoleService
        .submitEmulateText(this.emulateText, this.langType)
        .then((_res) => {
          this.emulateText = "";
          this.isCommandDisabled = false;
        })
        .catch((err) => {
          ElNotification({
            title: local.localeTrans('err'),
            message: `${local.localeTrans('exe')} ${this.langType} ${local.localeTrans('ins_fail')}`,
            type: "error"
          });
          this.isCommandDisabled = false;
          console.log(err);
        });
    }
  }
});
</script>
<style lang="scss">
.sim-console {
  height: 100%;
  position: relative;

  ._panel-content {
    box-sizing: content-box;
    padding: 5px 16px;
    overflow-x: hidden;
    overflow-y: auto;
    height: calc(100% - 38px);
    >div:last-child{
      margin-bottom: 5px;
    }
    .__log-time {
      color: #969899;
    }

    .warn-text {
      color: #dea32c;
    }

    .error-text {
      color: red;
    }
  }

  ._toolbar {
    height: 30px;
    padding-left: 10px;
    border-bottom: 1px solid #dcdcdc;
    display: flex;
    align-items: center;

    .__spacer {
      margin: 0 6px;
      width: 1px;
      height: 16px;
      display: inline-block;
      background: #dcdcdc;
    }

    ._action-btn {
      line-height: 20px;
      display: inline-flex;
      align-items: center;
      padding: 0 4px;

      &:hover {
        background: linear-gradient(107deg, #a3dbff 0%, #74c5f8 100%);
      }

      &:active {
        background: linear-gradient(107deg, #7fc8f6 0%, #3ea5e6 100%);
      }

      img {
        margin-right: 0;
      }
    }

    ._toolbar-icon {
      position: relative;
    }
  }

  .bottom_input {
    width: 100%;
    position: absolute;
    bottom: 2px;
    height: 30px;
    display: flex;
    line-height: 30px;
    border-top: 1px solid #E4E4E4;
    background-color: #fff;
    .prepend {
      font-size: 11px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.65);
      width: 80px;
      text-align: center;
      padding-left: 15px;

      >span {
        margin-right: 10px;
      }
    }

    .append {
      font-size: 12px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: rgba(0, 0, 0, 0.65);
      width: 110px;
      text-align: center;
      cursor: pointer;

      img {
        margin-left: 5px;
      }

      border-left: 1px solid #E4E4E4;
    }

    .el-input {
      .el-input__inner {
        border: none;
        padding-left: 5px;
      }
    }
  }
}
</style>
